# = test.forum.1.rb - Forums , Create Forum Test
# 
# See LICENSE.txt for licensing details.
#
# == Description
# In this test the functionality for creating a new forum post under sample forums is tested and verified
# 
#
require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'
    
	class TC_CreateForum_test < Test::Unit::TestCase
    
    def startup
      $PostSubject = randomChar(20)
      $PostBody = randomChar(50)
      $PostTag = randomChar(10)
      @@ie = Watir::IE.start($SITE_URL)
	  end
	
	  def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
	  end
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
      ie.link(:text, 'Sign in').click
      assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
      ie.text_field(:id, /username$/).value = $ADMIN_USER
      assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
      ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
      assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
      ie.link(:id, /loginButton$/).click
      test_01_verifyHomeText
    end

    def test_06_verifyLogoutLinkExists
      assert_equal $SITE_URL + 'logout.aspx', ie.link(:text, 'Sign out').href
    end
    
    def test_07_navigateToForum
      assert_equal $SITE_URL +'forums/', ie.link(:text, 'Forums').href
      ie.link(:text, 'Forums').click
      assert(ie.link(:text, /Sample Forum$/).exists?)
      ie.link(:text, /Sample Forum$/).click
    end
    
    def test_08_navigateToWriteNewPost
      assert(ie.link(:text, /Write a New Post/).exists?)
      ie.link(:text, /Write a New Post/).click
      assert(ie.contains_text("Write a New Post"))
    end
    
    def test_09_writePostAndSubmit
      assert(ie.text_field(:id, /PostSubject$/).exists?)
      ie.text_field(:id, /PostSubject$/).value = $PostSubject
      
      assert(ie.text_field(:id, /Editor$/).exists?)
      
      ie.ie.Document.parentWindow.execScript("tinyMCE.getInstanceById(tinyMCE.getEditorId('mce_editor_0')).execCommand('mceSetContent', false, '" + $PostBody + "')")
      
      assert(ie.text_field(:id, /Tags$/).exists?)
      ie.text_field(:id, /Tags$/).value = $PostTag
      assert(ie.button(:id, /PostButton$/).exists?)
      ie.button(:id, /PostButton$/).click
      sleep 30
    end
    
    def test_10_verifyPostExists
      assert(ie.contains_text($PostSubject))
      assert(ie.contains_text($PostBody))
      assert(ie.link(:text, $PostTag).exists?)
    end
      
    def test_11_verifyForumButtonsExist
      assert(ie.link(:text, "Reply").exists?)
      assert(ie.link(:text, "Delete").exists?)
      assert(ie.link(:text, "Edit").exists?)
      assert(ie.link(:text, "Favorites").exists?)
      assert(ie.link(:text, "Contact").exists?)
      assert(ie.link(:text, "Moderate").exists?)
      assert(ie.link(:text, "Previous").exists?)
      assert(ie.link(:text, "Next").exists?)
      assert(ie.link(:text, "Reply").exists?)
      assert(ie.link(:text, "Change View").exists?)
    end
    
    def test_12_verifyLinksExist
      assert(ie.link(:text, 'admin').exists?)
      assert(ie.link(:text, 'Sign out').exists?)
      assert(ie.link(:text, 'Help').exists?)
      assert(ie.link(:text, 'Inbox').exists?)
      assert(ie.link(:text, 'Home').exists?)
      assert(ie.link(:text, 'Blogs').exists?)
      assert(ie.link(:text, 'Forums').exists?)
      assert(ie.link(:text, 'Photos').exists?)
      assert(ie.link(:text, 'Downloads').exists?)
      assert(ie.link(:text, 'Control Panel').exists?)
      assert(ie.link(:text, 'Select Tags...').exists?)
      assert(ie.link(:text, 'Save').exists?)
      assert(ie.link(:text, 'Cancel').exists?)
      assert(ie.link(:text, 'Change View').exists?)
      assert(ie.link(:text, 'Previous').exists?)
      assert(ie.link(:text, 'Next').exists?)
      assert(ie.link(:url, '').exists?)
      assert(ie.link(:text, 'Reply').exists?)
      assert(ie.link(:text, 'Delete').exists?)
      assert(ie.link(:text, 'Edit').exists?)
      assert(ie.link(:text, 'Favorites').exists?)
      assert(ie.link(:text, 'Contact').exists?)
      assert(ie.link(:text, 'Moderate').exists?)
      assert(ie.link(:text, '[Edit Tags]').exists?)
    end
    
    def test_13_verifyLinksCorrect
      assert_equal $SITE_URL + 'logout.aspx', ie.link(:text, 'Sign out').href
      assert_equal $SITE_URL + 'user/PrivateMessages/default.aspx', ie.link(:text, 'Inbox').href
      assert_equal $SITE_URL, ie.link(:text, 'Home').href
      assert_equal $SITE_URL + 'forums/', ie.link(:text, 'Forums').href
    end
    
    def test_14_verifyFileUnderCorrectForum
      assert(ie.link(:text, "Sample Forum").exists?)
      ie.link(:text, "Sample Forum").click
      assert(ie.link(:text, $PostSubject).exists?)
    end
  
    
  end